package org.example.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.ResultCode.JwtUtils;
import org.example.ResultCode.MD5;
import org.example.educenter.entity.UcenterMember;
import org.example.educenter.entity.vo.RegisterVo;
import org.example.educenter.mapper.UcenterMemberMapper;
import org.example.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lining
 * @since 2023-11-05
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
     @Autowired
     private RedisTemplate<String,String> redisTemplate;
     @Override
     public String login(UcenterMember member){
          //.获取手机号和密码
          String mobile=member.getMobile();
          String password=member.getPassword();
          if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password) ){
               throw new GuliException(20001,"手机号，密码为空");
          }
          QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
          wrapper.eq("mobile",mobile);
          UcenterMember mobileMember=baseMapper.selectOne(wrapper);
          if(mobileMember==null){
               throw new GuliException(20001,"没有这个手机号数据");
          }
          if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
               throw new GuliException(20001,"密码错误");
          }
          if(mobileMember.getIsDisabled()){
               throw new GuliException(20001,"用户已禁用");
          }
          //生成taken 自符串
          String jwtToken= JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
          return  jwtToken;

     }
     @Override
     public void register(RegisterVo registerVo){
          //获取注册的数据
          String nickname=registerVo.getNickname();
          String mobile = registerVo.getMobile(); //手机号
          String password = registerVo.getPassword(); //密码
          String code = registerVo.getCode(); //验证码
          if(StringUtils.isEmpty(nickname)||StringUtils.isEmpty(mobile)||
                  StringUtils.isEmpty(password)||StringUtils.isEmpty(code)){
               throw  new GuliException(20001,"注册失败");
          }
          //获取redis中的验证码
          String rediscode=redisTemplate.opsForValue().get(mobile);
          if(!code.equals(rediscode)){
               throw  new GuliException(20001,"注册失败");
          }
          QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
          wrapper.eq("mobile",mobile);
          Integer count=baseMapper.selectCount(wrapper);
          if(count>0){
               throw  new GuliException(20001,"注册失败");
          }
          //注册成功添加到数据表中
          UcenterMember member=new UcenterMember();
          member.setNickname(nickname);
          member.setMobile(mobile);
          member.setPassword(MD5.encrypt(password));
          member.setIsDisabled(false); //用户未禁用
          member.setAvatar("https://edu-mxy.oss-cn-hangzhou.aliyuncs.com/2022/08" +
                  "/28/e97af8298b4c481695cc7723c01c614a1243.jpg"); //用户默认头像
          baseMapper.insert(member);


     }
     //根据openi查看数据库是否有这个人的信息
//     扫码登陆
     @Override
     public UcenterMember getOpenIdMember(String openid){
          QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
          wrapper.eq("openid",openid);
          UcenterMember member=baseMapper.selectOne(wrapper);
          return member;

     }
     //查询某天注册人数(该方法供远程调用使用)
     @Override
     public Integer countRegisterDay(String day){
          return  baseMapper.countRegisterDay(day);
     }

}
